<?php
namespace app\common\model;

use think\Model;
use hg\apidoc\annotation\Field;
use hg\apidoc\annotation\WithoutField;
use hg\apidoc\annotation\AddField;
use hg\apidoc\annotation\Param;

class SuperUser extends Model
{
    protected $table = 'super_user';
    public function role(){
        return $this->hasOne(Role::class,'id','role_id');
    }
    /**
     * 保存新添加的超级用户
     */
    public function saveSuperUser($data){
        if (empty($data) || !is_array($data)) return false;
        return $this->save($data);
    }
    /**
     * 更新超级用户
     */
    public function updateSuperUser($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update($data);
    }
    /**
     * 修改超级用户状态
     */
    public function statusSuperUser($data){
        if (empty($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['status' => $data['status']]);
    }
    /**
     * 获取超级用户列表
     * @WithoutField("password")
     */
    public function getSuperUserList($data, $super_user_auth)
    {
        if (empty($data)) return false;
        $where = [
            ['id', '>', 1],
            ['status' ,'>' , $super_user_auth['id'] === 1 ? -20000 : -1],
            ['username', 'like', '%' . $data['username'] . '%']
        ];
        $order = [
            'id'=>'asc'
        ];
        $result = [
            'list' => $this->where($where)->limit($data['pageRow'])->page($data['pageIndex'])->order($order)->withoutField('password')->select(),
            'count' => $this->where($where)->order($order)->count()
        ];
        return $result;
    }
    /**
     * 通过用户名查找超级用户 
     */
    public function getSuperUserByUsername($username){
        if (empty($username)){
            return false;
        }
        return $this->where(['username'=>trim($username)])->find();
    }
    /**
     * 通过邮箱查找超级用户 
     */
    public function getSuperUserByEmail($email){
        if (empty($email)){
            return false;
        }
        return $this->where(['email'=>trim($email)])->find();
    }
    /**
     * 分配角色
     */
    public function setRole($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['role_id' => $data['roleId']]);
    }
    /**
     * 保存用户头像
     */
    public function saveAvatar($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update(['avatar' => $data['avatar']]);
    }
    /**
     * 保存用户信息
     * @Field("nickname,office,introduction")
     */
    public function saveSuperUserInfo($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update([
            'nickname' => $data['nickname'],
            'office' => $data['office'],
            'introduction' => $data['introduction']
        ]);
    }
    /**
     * 保存用户邮箱
     */
    public function saveSuperUserEmail($data){
        if (empty($data) || !is_array($data)) return false;
        $where = [
            'id' => $data['id'],
        ];
        return $this->where($where)->update([
            'email' => $data['email']
        ]);
    }
}
